<template>
  <ul class="list">
    <li
      v-for="(item, index) in data"
      :key="index"
      @click="
        $router.push({
          path: '/product',
          query: {
            goodsId: item.goodsId,
            productId: item.productId || item.defaultProductId,
          },
        })
      "
    >
      <div class="msg">
        <img v-lazy="item.mainImage || item.goodsImage" alt="" />

        <p>{{ item.goodsName }}</p>
      </div>
      <div class="price">
        <span>￥{{ item.goodsPrice.toFixed(2) }}</span>
        <img
          src=" https://image-mall.gxptkc.com/m/resources/add-cart.png"
          alt=""
          @click.stop="addToCart(item.productId || item.defaultProductId)"
        />
      </div>
    </li>
  </ul>
</template>

<script>
import qs from "qs";
import Vue from "vue";
import { Toast } from "vant";
import { Lazyload } from "vant";

Vue.use(Lazyload);
Vue.use(Toast);
export default {
  data() {
    return {
      token: null,
    };
  },
  created() {
    this.token = localStorage.getItem("fastCar-token");
  },
  props: ["data"],
  methods: {
    async addToCart(id) {
      let res = await this.axios({
        url: "https://m-mall.gxptkc.com/v3/business/front/cart/add",
        method: "POST",
        headers: {
          authorization: `Bearer ${this.token}`,
          "content-type": "application/x-www-form-urlencoded",
        },
        data: qs.stringify({
          productId: id,
          number: 1,
        }),
      });
      Toast(res.data.msg);
    },
  },
};
</script>

<style lang="scss" scoped>
ul.list {
  padding: 0 8rem;
  box-sizing: border-box;
  display: flex;
  justify-content: left;
  flex-wrap: wrap;
  width: 100vw;
  // background-color: #eee;
  li {
    background-color: #fff;
    padding-bottom: 10rem;
    margin-bottom: 10rem;
    width: 48%;
    height: 280rem;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    box-sizing: border-box;
    border-radius: 10rem;
    overflow: hidden;
    box-shadow: 0 0 2rem 0rem #ccc;
    &:nth-child(2n) {
      margin-left: 14rem;
    }
    .msg {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      > img {
        width: 100%;
        height: 172.5rem;
      }
      > p {
        margin-top: 10rem;
        padding: 0 10rem 0 10rem;
        font-size: 14rem;

        width: 90%;
        display: -webkit-box;
        overflow: hidden;
        text-overflow: ellipsis;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
    }
    .price {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 10rem 0 10rem;
      box-sizing: border-box;
      span {
        font-size: 17rem;
        color: rgb(252, 28, 28);
      }
      img {
        width: 21rem;
      }
    }
  }
}
</style>